package com.dejian.bloodPressure;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.baidu.autoupdatesdk.BDAutoUpdateSDK;
import com.sam.dejian.PlatformPreference;
import com.sam.dialog.CustomAlertDialogBuilder;
import com.sam.gui.activity.WebLoginWebActivity;
import com.sam.gui.base.GuideActivity;
import com.sam.ui.base.activity.BaseActivity;
import com.sam.utils.NetUtil;

import chu.dejian.bloodPressure.R;
import chu.dejian.conf.BLEPreference;
import chu.dejian.dialogOrMenu.DialogManager;

public class SplashActivity extends BaseActivity {

    private CustomAlertDialogBuilder noNetworkMeasureOnlyDialog,notAllowedEnterDialog;

    @Override
    protected void onCreate(@Nullable Intent savedInstanceStateIntent) {
        setContentView(R.layout.activity_splash);
        BDAutoUpdateSDK.silenceUpdateAction(this);      // 静默更新
        noNetworkMeasureOnlyDialog = DialogManager.getNoNetworkMeasureOnlyDialog(this);
        notAllowedEnterDialog = DialogManager.getNotAllowedEnterDialog(this);

//        mLoginDialog = new LoadingDialog(this);
//        mLoginDialog.setLoadingText("正在检查更新……");
//        BDAutoUpdateSDK.uiUpdateAction(this, new MyUICheckUpdateCallback()); // 弹出对话框来让用户决定是否更新
//        BDAutoUpdateSDK.cpUpdateCheck(this, new MyCPCheckUpdateCallback());


        if(PlatformPreference.getInstance(this).getIsFirstRun()){
            startActivity(new Intent(SplashActivity.this,GuideActivity.class));
            PlatformPreference.getInstance(this).setIsFirstRun(false);
            finish();
            return;
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                if(NetUtil.checkNetWork(SplashActivity.this)) {
                    startActivity(new Intent(SplashActivity.this, WebLoginWebActivity.class));
                    finish();
                } else {
                    if(TextUtils.isEmpty(BLEPreference.getInstance().getToken(null))) notAllowedEnterDialog.show();
                    else noNetworkMeasureOnlyDialog.show();
                }
            }
        },1500);
    }

    @Override
    protected void onDestroy() {
        if(noNetworkMeasureOnlyDialog != null){
            if(noNetworkMeasureOnlyDialog.isShowing()) noNetworkMeasureOnlyDialog.dismiss();
            noNetworkMeasureOnlyDialog = null;
        }
        if(notAllowedEnterDialog != null){
            if(notAllowedEnterDialog.isShowing()) notAllowedEnterDialog.dismiss();
            notAllowedEnterDialog = null;
        }

        super.onDestroy();
    }

    @Override
    protected void findView() {

    }

    @Override
    protected void initView() {

    }

    private void parseSchemeData(){
        Intent intent = getIntent();
        String scheme = intent.getScheme();
        Uri uri = intent.getData();
        System.out.println("scheme:"+scheme);
        if (uri != null) {
            String host = uri.getHost();
            String dataString = intent.getDataString();
            String id = uri.getQueryParameter("d");
            String path = uri.getPath();
            String path1 = uri.getEncodedPath();
            String queryString = uri.getQuery();
            System.out.println("host:"+host);
            System.out.println("dataString:"+dataString);
            System.out.println("id:"+id);
            System.out.println("path:"+path);
            System.out.println("path1:"+path1);
            System.out.println("queryString:"+queryString);
        }
    }
}
